package com.zhuss.intention.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.intention.intention.SearchPersonalIntentionBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.intention.intention.DetailPersonalIntentionVo;
import com.zhuss.bean.vo.intention.intention.ListPersonalIntentionVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.processor.UserRead;
import com.zhuss.enums.SystemEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.intention.db.dao.ext.PersonalIntentionDao;
import com.zhuss.intention.db.entity.PersonalIntention;



/**
 * @description: 个人需求读
 * @author: zhushanshuo
 * @create: Fri Dec 25 09:39:45 CST 2020
 **/
@Component("personalIntentionRead")
public class PersonalIntentionRead {

    @Autowired
    private PersonalIntentionDao personalIntentionDao;
    
    @Autowired
    private UserRead userRead;


    public Map<String, PersonalIntention> listMap() {
        List<PersonalIntention> list = personalIntentionDao.listAll();
        Map<String, PersonalIntention> map = new HashMap<>();
        for (PersonalIntention c : list) {
            map.put(c.getPhone(),c);
        }
        return map;
    }

    /**
     * 个人需求
     *
     * @return
     */
    public Object listPersonalIntention() {
        List<PersonalIntention> personalIntentionList = personalIntentionDao.listAll();
        List<ListVo> result = new ArrayList<>();
        transferList(personalIntentionList,result);
        return result;
    }


    /**
     * 个人需求
     *
     * @return
     */
    public Object listPersonalIntention(List<Long> personalIntentionIdList) {
        List<PersonalIntention> result = personalIntentionDao.listById(personalIntentionIdList);
        return result;
    }


    /**
     * 根据名字查询个人需求
     *
     * @param
     * @return
     */
    public List<PersonalIntention> list(String name) {
        List<PersonalIntention> result = personalIntentionDao.listByName(name);
        return result;
    }

    public List<PersonalIntention> listNotId(String name,Long personalIntentionId) {
        List<PersonalIntention> result = new ArrayList<PersonalIntention>();
        List<PersonalIntention> list = list(name);
        for (PersonalIntention c : list) {
            if (!c.getIntentionId().equals(personalIntentionId)) {
                result.add(c);
                break;
            }
        }
        return result;
    }

    /**
     * 个人需求
     *
     * @return
     */
    public Object listPersonalIntentionId() {
        List<PersonalIntention> personalIntentionList = personalIntentionDao.listAll();
        List<Long> result = new ArrayList<>();
        transferListId(personalIntentionList,result);
        return result;
    }

    private void transferListId(List<PersonalIntention> personalIntentionList,List<Long> result) {
        for (PersonalIntention item : personalIntentionList) {
            result.add(item.getIntentionId());
        }
    }

    private void transferList(List<PersonalIntention> personalIntentionList,List<ListVo> result) {
        for (PersonalIntention item : personalIntentionList) {
            ListVo vo = new ListVo();
            vo.setKey(item.getIntentionId());
            vo.setLabel(item.getPhone());
            result.add(vo);
        }

    }

    /**
     * 个人需求列表
     *
     * @param bo
     * @return
     */
    public Object list(SearchPersonalIntentionBo bo) {
        PageVo<ListPersonalIntentionVo> result = new PageVo<ListPersonalIntentionVo>();
        Integer system = bo.getConcurrentSystem();
        if (SystemEnum.APP.sameKey(system)) {
            bo.setCreatorId(bo.getConcurrentUserId());
        }
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        List<ListPersonalIntentionVo> intentionVos = personalIntentionDao.list(bo);
        result.setItems(trainsfer(intentionVos));
        result.setTotal(personalIntentionDao.countList(bo));
        return result;
    }

    private List<ListPersonalIntentionVo> trainsfer(
			List<ListPersonalIntentionVo> intentionVos) {
    	intentionVos.forEach(vo -> {
			Long userId = vo.getCreatorId();
			DetailUserVo user;
			try {
				user = (DetailUserVo) userRead.detail(userId);
				String phone = user.getPhone();
				vo.setPhone(phone);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return intentionVos;
	}

	/**
     * 个人需求详情
     *
     * @param personalIntentionId
     * @return
     * @throws DzcxException
     */
    public Object detail(Long personalIntentionId) throws DzcxException {
        DetailPersonalIntentionVo vo = personalIntentionDao.detail(personalIntentionId);
        if (vo == null) {
            throw new DzcxException(ExceptionCode.数据不存在);
        } 
        return vo;
    }


}
